import './App.css';
import React from 'react';

function Square(props) {
  return (
    <button className='square' onClick={props.onClick}>
      {props.value}
    </button>
  )
}

class Board extends React.Component {

  renderSquare(i) {
    return (
      <Square
        key = {i}
        value={this.props.squares[i]}
        onClick={() => this.props.onClick(i)}
      />
    )
  }

  render() {
    const arr = Array(3).fill(null)
    return (
      <div>{
        arr.map((item, index) => {
          return (<div className="board-row" key={index}>
              {
                arr.map((item, index1) => {
                  const i = index * 3 + index1
                  return this.renderSquare(i)
                })
              }
          </div>)
        })
        }
      
        
      </div>
    );
  }
}

class Game extends React.Component {
  constructor(prors) {
    super(prors)
    this.state = {
      history: [{
        squares: Array(9).fill(null),
        steps:''
      }],
      xIsNext: true, //判断是否为X用户
      stepNumber: 0,
      hasWin: false
    }
  }

  handleClick(i) {
    const history = this.state.history.slice(0, this.state.stepNumber + 1)
    const current = history[history.length - 1]
    const squares = current.squares.slice()
    const steps = i
    if(calculateWinner(squares) || squares[i]) {
      return
    }
    squares[i] = this.state.xIsNext ? 'X' : 'O'
    this.setState({
      history: history.concat([{
        squares: squares,
        steps: steps
      }]),
      xIsNext: !this.state.xIsNext,
      stepNumber: history.length
    })
  }

  jumpTo(step) {
    for (let i = 0; i < 9; i++) {
      document.getElementsByClassName('square')[i].style = ''
    }
    this.setState({
      stepNumber: step,
      xIsNext: (step % 2) === 0,
    });
  }

  reverse(){
    let history = this.state.history
    let flag = history.length - 1
    if(history[0].steps === ''){
      flag = 0
    }
    this.setState({
        history: history.reverse(),
        stepNumber: flag
    })
    
  }

  render() {
    const history = this.state.history
    const current = history[this.state.stepNumber];
    const winner = calculateWinner(current.squares)
    const arr = [[1,1], [1.2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]]

    const moves = history.map((step, move) => {
      let flag = true
      if(step.steps === '') flag = false
      const info = flag ? 
      `GO TO : ${move} (${arr[step.steps]})` :
      'GO TO START'
       const nowStep = this.state.stepNumber
      if(nowStep === move){
        return(
          <li className='nowStep' key={move}>
              <button onClick={() => this.jumpTo(move)}>{info}</button>
          </li>
        )
      }
      return (
        <li key={move}>
        <button onClick={() => this.jumpTo(move)}>{info}</button>
      </li>
      )
    })
    let status
    console.log(history);
    if (winner) {
      status = 'WINNER: ' + winner.winner
      for (const item of  winner.steps) {
        document.getElementsByClassName('square')[item].style = 'background : #abc; color : #fff'
      }} else if (history.length > 9){
        status = 'NO BODY WIN'
      } else {
      status = 'NEXT PLAYER : ' + (this.state.xIsNext ? 'X' : 'O')
    }

    return (
      <div className="game">
        <div className="game-board">
          <Board squares={current.squares} onClick ={i => this.handleClick(i)} />
        </div>
        <div className="game-info"> 
        <div>{status}</div>
        <button onClick={() => this.reverse()}>反转</button>
          <ol>{moves}</ol>  
        </div>
       
      </div>
    );
  }
}


function calculateWinner(squares) {
  const lines = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ];
  for (let i = 0; i < lines.length; i++) {
    const [a, b, c] = lines[i];
    if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
      return {
        steps: [a,b,c],
        winner: squares[a]
      }
    }
  }
  return null;
}

export default Game;
